﻿
namespace Webinator.ExtensionMethods
{
    using System.Drawing;
    using System.Threading;
    using Util;

    /// <summary>
    /// Useful extension methods.
    /// </summary>
    public static class RectangleExtensions
    {
        public static Rectangle GetWindowCoordinates(this Rectangle value, IWebManager webManager)
        {
            return new Rectangle(value.X + webManager.ViewportCoordinates.X, value.Y + webManager.ViewportCoordinates.Y, value.Width, value.Height);
        }

        public static Point GetWindowCoordinates(this Point value, IWebManager webManager)
        {
            return new Point(value.X + webManager.ViewportCoordinates.X, value.Y + webManager.ViewportCoordinates.Y);
        }

        public static Rectangle GetScreenCoordinates(this Rectangle value, IWebManager webManager)
        {
            var windowCoordinates = value.GetWindowCoordinates(webManager);
            var webPosition = WindowHelper.GetWindowPosition(webManager.Process.MainWindowHandle);
            return new Rectangle(windowCoordinates.X + webPosition.X, windowCoordinates.Y + webPosition.Y, windowCoordinates.Width, windowCoordinates.Height);
        }

        public static Point GetScreenCoordinates(this Point value, IWebManager webManager)
        {
            var windowCoordinates = value.GetWindowCoordinates(webManager);
            var webPosition = WindowHelper.GetWindowPosition(webManager.Process.MainWindowHandle);
            return new Point(windowCoordinates.X + webPosition.X, windowCoordinates.Y + webPosition.Y);
        }

        public static Point GetMidpoint(this Rectangle value)
        {
            return new Point(value.X + (value.Width / 2), value.Y + (value.Height / 2));
        }

        public static void Click(this Rectangle value, IWebManager webManager)
        {
            WindowHelper.SetActiveWindow(webManager.Process.MainWindowHandle);
            SendInputClass.ClickLeftMouseButton(value.X, value.Y);   
        }

        public static void Click(this Point value, IWebManager webManager)
        {
            WindowHelper.SetActiveWindow(webManager.Process.MainWindowHandle);
            SendInputClass.ClickLeftMouseButton(value.X, value.Y);   
        }

        public static void DoubleClick(this Point value, IWebManager webManager)
        {
            WindowHelper.SetActiveWindow(webManager.Process.MainWindowHandle);
            SendInputClass.DoubleClickLeftMouseButton(value.X, value.Y);
        }

        public static void ClickDrag(this Point value, Point destination, IWebManager webManager)
        {
            WindowHelper.SetActiveWindow(webManager.Process.MainWindowHandle);
            SendInputClass.MouseDown(value.X, value.Y);
            Thread.Sleep(250);
            SendInputClass.ClickLeftMouseButton(destination.X, destination.Y);   
        }

        public static void MouseMove(this Point value, IWebManager webManager)
        {
            WindowHelper.SetActiveWindow(webManager.Process.MainWindowHandle);
            SendInputClass.MouseMove(value.X, value.Y);
        }
    }
}